<template>
    <div>
        <el-card class="!border-none mb-4" shadow="never">
            <el-form :model="queryParams" class="mb-[-16px]" inline>
                <el-form-item label="订单号" prop="sn">
                    <el-input v-model="queryParams.sn" class="w-[280px]" clearable placeholder="请输入" />
                </el-form-item>
                <el-form-item label="用户手机号" prop="user_mobile">
                    <el-input v-model="queryParams.user_mobile" class="w-[280px]" clearable placeholder="请输入用户手机号" />
                </el-form-item>
                <el-form-item class="w-[280px]" label="支付状态" prop="pay_status">
                    <el-select v-model="queryParams.pay_status" clearable placeholder="请选择">
                        <el-option label="未支付" value="1" />
                        <el-option label="已支付" value="2" />
                        <el-option label="已取消" value="3" />
                    </el-select>
                </el-form-item>
                <el-form-item class="w-[280px]" label="订单状态" prop="status">
                    <el-select v-model="queryParams.status" clearable placeholder="请选择">
                        <el-option label="正常" value="1" />
                        <el-option label="进行中" value="2" />
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="resetPage">查询</el-button>
                    <el-button @click="resetParams">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>
        <el-card v-loading="pager.loading" class="!border-none" shadow="never">
            <div class="mt-4">
                <el-table :data="pager.lists">
                    <el-table-column label="订单号" prop="sn" show-overflow-tooltip width="200" />
                    <el-table-column label="支付价格" prop="pay_price" show-overflow-tooltip />
                    <el-table-column label="使用时间" prop="use_time" show-overflow-tooltip />
                    <el-table-column label="支付状态" prop="pay_status" show-overflow-tooltip>
                        <template #default="{ row }">
                            <el-tag>{{ row.pay_status == 1 ? '未支付' : row.pay_status == 2 ? '已支付' : '已取消' }}
                            </el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column label="订单状态" prop="status" show-overflow-tooltip>
                        <template #default="{ row }">
                            <el-tag type="info">
                                {{ row.status == 1 ? '正常' : row.status == 2 ? '进行中' : row.status == 3 ? '已结束' :
                                    row.status == 4 ? '异常' : '已结算'
                                }}
                            </el-tag>
                        </template>
                    </el-table-column>

                    <el-table-column label="退款状态" prop="refund_status" show-overflow-tooltip>
                        <template #default="{ row }">
                            <el-tag>
                                {{ row.refund_status == 1 ? '未发起' : row.refund_status == 2 ? '已发起' : row.refund_status
                                    == 3 ? '已退款' : row.refund_status == 4 ? '退款失败' : ''
                                }}
                            </el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column label="支付类型" prop="pay_type" show-overflow-tooltip>
                        <template #default="{ row }">
                            <el-tag>
                                {{ row.pay_type == 1 ? '余额支付' : row.pay_type == 2 ? '微信支付' : row.pay_type == 3 ? '混合支付'
                                    : ''
                                }}
                            </el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column label="用户手机号" prop="user_mobile" show-overflow-tooltip />
                    <el-table-column label="支付时间" prop="pay_time" show-overflow-tooltip width="180px">
                        <template #default="{ row }">
                            {{ row.pay_time ? moment(row.pay_time * 1000).format('YYYY-MM-DD HH:mm:ss') : '' }}
                        </template>
                    </el-table-column>
                    <el-table-column label="收入" prop="income" show-overflow-tooltip />
                    <el-table-column fixed="right" label="操作" width="120">
                        <template #default="{ row }">
                            <el-button v-perms="['boom_order/edit']" link type="primary" @click="handleEdit(row)">
                                详情
                            </el-button>
                            <el-button v-perms="['boom_order/refund']" link type="danger"
                                :disabled="row.refund_status == 1 ? false : true" @click="handleRefund(row)">
                                退款
                            </el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <div class="flex mt-4 justify-end">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
        <edit-popup v-if="showEdit" ref="editRef" :dict-data="dictData" @close="showEdit = false" @success="getLists" />
        <el-dialog v-model="centerDialogVisible" title="订单退款" width="500" align-center>
            <el-form>
                <el-form-item :model="refundInfo" label="退款金额">
                    <el-input v-model="refundInfo.refund_money" type="number" />
                </el-form-item>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button @click="centerDialogVisible = false">取消</el-button>
                    <el-button type="primary" @click="doRefund()">
                        确认
                    </el-button>
                </div>
            </template>
        </el-dialog>
    </div>
</template>

<script lang="ts" name="boomOrderLists" setup>
import { usePaging } from '@/hooks/usePaging'
import { useDictData } from '@/hooks/useDictOptions'
import { apiBoomOrderLists, refundOrder } from '@/api/boom_order'
import moment from 'moment'
import EditPopup from './edit.vue'
import { ElMessage } from 'element-plus'

const editRef = shallowRef<InstanceType<typeof EditPopup>>()
// 是否显示编辑框
const showEdit = ref(false)
const centerDialogVisible = ref(false)

// 查询条件
const queryParams = reactive({
    sn: '',
    user_mobile: '',
    pay_status: '',
    status: ''
})

const refundInfo = reactive({
    order_id: '',
    refund_money: ''
})

// 获取字典数据
const { dictData } = useDictData('')

// 分页相关
const { pager, getLists, resetParams, resetPage } = usePaging({
    fetchFun: apiBoomOrderLists,
    params: queryParams
})


// 编辑
const handleEdit = async (data: any) => {
    showEdit.value = true
    await nextTick()
    editRef.value?.getDetail(data)
    editRef.value?.open()
}

const handleRefund = (data: any) => {
    refundInfo.refund_money = ''
    centerDialogVisible.value = true
    refundInfo.order_id = data.id
}

const doRefund = async () => {
    await refundOrder(refundInfo)
    ElMessage({
        type: 'success',
        message: '操作成功'
    })
    centerDialogVisible.value = false
    getLists()

}

getLists()
</script>
